package com.example.inter.controller;

import com.example.inter.exception.ServiceException;
import com.example.inter.converter.InterConverter;
import com.example.inter.mapper.InterMapper;
import com.example.inter.mapper.PartMapper;
import com.example.inter.pojo.Part;
import com.example.inter.vo.*;
import com.example.inter.pojo.Inter;
//import com.example.inter.service.InterService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wuwenze.poi.ExcelKit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.MDC;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author  SongPeng
 * @date  2021/11/10 14:19
 * @version 1.0
 *
 */

@RestController
@RequestMapping("/Inter")
public class InterController {
    @ModelAttribute
    public void doString(){
        MDC.put("operateModuleName","内部人员管理模块");
    }
//    @Autowired
//    private InterService interService;
    @Autowired
    private InterMapper interMapper;
    @Autowired
    private InterConverter interConverter;
    @Autowired
    private PartMapper partMapper;

    private static final Logger logger = LogManager.getLogger(InterController.class);
    //查询内部人员信息列表
    @RequestMapping("/listData")
    public PageVO<InterVo> listData(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize,InterVo interVo){
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(Inter.class);
        String interName = interVo.getInterName();
        Integer interSex = interVo.getInterSex();
        Long partId=interVo.getPartId();
        Example.Criteria criteria = o.createCriteria();
        if(interName!=null&&!"".equals(interName)){
            criteria.andLike("interName","%"+interName+"%");
        }
        if(interSex!=null){
            criteria.andEqualTo("interSex",interSex);
        }
        if(partId!=null){
            criteria.andEqualTo("partId",partId);
        }
        List<Inter> interList = interMapper.selectByExample(o);
        List<InterVo> interVOS = interConverter.converterToInterVOList(interList);
        PageInfo<Inter> info=new PageInfo<>(interList);
        return new PageVO<>(info.getTotal(),interVOS);
    }
    /**
     * 删除内部人员
     *
     * */
    @DeleteMapping("/delete/{interId}")
    public void deleteinter(@PathVariable("interId") Long interId){
        logger.info("删除内部人员");
        interMapper.deleteByPrimaryKey(interId);


    }
    /**
     * 增加内部人员
     *
     * */
    @PostMapping("/add")
    public void add(@RequestBody @Validated InterVo interVo) {
        logger.info("增加内部人员");
//        interService.add(interVo);
//        return ResponseBean.success();
        String interName = interVo.getInterName();
        Long departmentId = interVo.getPartId();
        Example o = new Example(Inter.class);
        o.createCriteria().andEqualTo("interName",interName);
        //根据Condition(对象属性)条件进行查询总数
        int i = interMapper.selectCountByExample(o);
        if(i!=0){
            throw new ServiceException("该用户名已被占用");
        }
        Part department = partMapper.selectByPrimaryKey(departmentId);
        if(department==null){
            throw new ServiceException("该部门不存在");
        }
        Inter inter=new Inter();
        BeanUtils.copyProperties(interVo,inter);
        interMapper.insertSelective(inter);
    }
    /**
     * 更新内部人员信息
     *
     * @param interId
     * @param interVo
     * @return
     */
    @PutMapping("/update/{interId}")
    public void update(@PathVariable Long interId, @RequestBody @Validated InterVo interVo) {
        logger.info("更新内部人员信息");
        Inter dbinter = interMapper.selectByPrimaryKey(interId);
        @NotBlank(message = "用户名不能为空") String interName = interVo.getInterName();
        @NotNull(message = "部门不能为空") Long departmentId = interVo.getPartId();
        if(dbinter==null){
            throw new ServiceException("要删除的部门人员不存在");
        }
        Part part = partMapper.selectByPrimaryKey(departmentId);
        if(part==null){
            throw new ServiceException("该部门不存在");
        }
        Example o = new Example(Inter.class);
        o.createCriteria().andEqualTo("interName",interName);
        List<Inter> inters = interMapper.selectByExample(o);
        if(!CollectionUtils.isEmpty(inters)){
            if(!inters.get(0).getInterId().equals(interId)){
                throw new ServiceException("该内部人员已存在");
            }
        }
        Inter inter=new Inter();
        BeanUtils.copyProperties(interVo,inter);
        inter.setInterId(dbinter.getInterId());
        //根据主键更新属性不为null的值
        interMapper.updateByPrimaryKeySelective(inter);
    }

    /**
     * 编辑内部人员
     * @param interId
     * @return
     */
    @GetMapping("/edit/{interId}")
    public InterVo edit(@PathVariable Long interId) {
        logger.info("编辑内部人员");
        Inter inter = interMapper.selectByPrimaryKey(interId);
        if(inter==null){
            throw new ServiceException("要编辑的内部人员不存在");
        }
        InterVo interVo=new InterVo();
        BeanUtils.copyProperties(inter,interVo);
        Part part = partMapper.selectByPrimaryKey(inter.getPartId());
        if(part!=null){
            interVo.setPartId(part.getPartId());
        }
        interVo.setPartName(part.getPartName());
        return interVo;
    }
    /**
     * 导出内部人员excel
     * @param response
     */
    @PostMapping("/excel")
    public List<Inter> export(HttpServletResponse response) {
        logger.info("导出内部人员表格");

        return interMapper.selectAll();
    }

}
